package njust.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import njust.dao.GoodsDao;
import njust.domain.Goods;

public class GoodsDaoImpl extends HibernateDaoSupport implements GoodsDao {
	
	@Override
	public List searchGoods(String keywords,String style) {
		String res="Time";
		if(style.equals("0"))
			res="Time";
		else if(style.equals("1"))
			res="Price";
		else
			res="Pageview";
		List list = this.getHibernateTemplate().find(
				"from Goods where GoodName like '%"+keywords +"%' order by "+res+" desc" );
		return list;
	}

	@Override
	public List getGoodByUserId(int id) {
		List list = new ArrayList();
		list = this.getHibernateTemplate()
				.find("from Goods where userid="+id+" order by Time desc ");
		return list;
	}

	@Override
	public boolean addGoods(Goods goods) {
		this.getHibernateTemplate().save(goods);
		return true;
	}

	@Override
	public boolean updateGoods(Goods goods) {
		this.getHibernateTemplate().update(goods);
		return true;
	}

	@Override
	public boolean deleteGoods(int id) {
		Goods goods = (Goods) this.getHibernateTemplate().get(Goods.class, id);
		this.getHibernateTemplate().delete(goods);
		return true;
	}

	@Override
	public List getAllGoods() {
		List list = new ArrayList();
		// this.getHibernateTemplate().setCacheQueries(true);
		list = this.getHibernateTemplate()
				.find("from Goods order by Time desc");
		return list;
	}

	@Override
	public Goods getGoodById(int id) {
		System.out.println("hei before");
		Goods goods = this.getHibernateTemplate().get(Goods.class, id);
		System.out.println("hei after");
		return goods;

	}

	@Override
	public Goods getGoodByName(String name) {
		List list = this.getHibernateTemplate().find(
				"from Goods where GoodName=?", name);
		Goods product = (Goods) list.get(0);
		return product;
	}

	@Override
	public List searchGoods(String keywords) {
		List list = this.getHibernateTemplate().find(
				"from Goods where GoodName like '%"+keywords +"%'" );
		return list;
	}
	
}
